//index.js
//获取应用实例
const app = getApp()
// http://39.97.33.178/api/movieOnInfoList?cityId=10
Page({
  data: {
    movieList: [],
    currentCity: []
  },
  onReady() {
    //初始化调用
    //获取当前城市
    this.getLocation();
    app.globalData.callBackEvent = () => {
      var currentCity = app.globalData.currentCity;
      this.setData({
        currentCity
      })
      this.movieOnInfoList(currentCity.id);
    }

  },
  toPath(e) {
    wx.navigateTo({
      url: '/pages/detail/detail?id=' + e.currentTarget.dataset.id,
    })
  },
  movieOnInfoList(id) {
    wx.request({
      url: 'http://www.h5yjy.com/api/movieOnInfoList?cityId=' + id,
      success: (res) => {
        var movieList = this.data.movieList;
        movieList = res.data.data.movieList;
        movieList.map((item) => {
          item.img = item.img.replace("w.h", "66.94");
        })
        this.setData({
          movieList
        });
      }
    })
  },
  getLocation() {
    wx.request({
      url: 'http://www.h5yjy.com/api/getLocation', //获取当前城市
      success: (res) => {
        var currentCity = res.data.data; // {nm:"北京",id:111}
        // 把值给全局globalcity
        app.globalData.currentCity = currentCity;
        // 根据当前城市id,获取当前城市 数据
        this.movieOnInfoList(currentCity.id)
        this.setData({
          currentCity
        }); //更新视图的
      }
    })
  },
  tosearch() {
    wx.navigateTo({
      url: '/pages/search/search',
    })
  },
  toCity() {
    wx.navigateTo({
      url: '/pages/city/city',
    })
  }
})